package com.tech.config;

import com.alibaba.druid.pool.DruidDataSource;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;

import javax.sql.DataSource;


//读取properties 配置文件 将此配置文件读到 Spring容器当中
@PropertySource("classpath:jdbc.properties")

public class DataSourceConfiguration {

    //从Spring 容器中读取 key
    @Value("${jdbc.driver}")
    private String driver;

    //从Spring 容器中读取 key
    @Value("${jdbc.url}")
    private String url;

    //从Spring 容器中读取 key
    @Value("${jdbc.username}")
    private String username;

    //从Spring 容器中读取 key
    @Value("${jdbc.password}")
    private String password;

    @Bean("dataSource")
    //将返回值 放入到Spring 容器中
    public DataSource getDataSoure() {
        DruidDataSource druidDataSource = new DruidDataSource();
        druidDataSource.setDriverClassName(driver);
        druidDataSource.setUrl(url);
        druidDataSource.setUsername(username);
        druidDataSource.setPassword(password);
        return druidDataSource;
    }
}
